/**
 * 部门管理初始化
 */
var PaymentTotal = {
    id: "PaymentTotalTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
PaymentTotal.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},

        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {
            title: 'reservationsId',
            field: 'reservationsId',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: '50px'
        },
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,},
        {title: '客户电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true,},
        {
            title: '客户性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                }  else {
                    return "未知"
                }
            }
        },
        {title: '客户年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true,},
        {title: '客户状态', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true,},
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true,},
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true,},
        // {title: '化名', field: 'pseudonym', align: 'center', valign: 'middle', sortable: true,},
        {title: '现金收费', field: '现金收费', align: 'center', valign: 'middle', sortable: true,},
        {title: '扫码支付', field: '扫码支付', align: 'center', valign: 'middle', sortable: true,},
        {title: '刷卡', field: '刷卡', align: 'center', valign: 'middle', sortable: true,},
        {title: '美团', field: '美团', align: 'center', valign: 'middle', sortable: true,},
        {title: '积分抵扣', field: '积分抵扣', align: 'center', valign: 'middle', sortable: true,},
        {title: '华荣时代', field: '华荣时代', align: 'center', valign: 'middle', sortable: true,},
        {title: '优惠券', field: '优惠券', align: 'center', valign: 'middle', sortable: true,},
        {title: '医保', field: '医保', align: 'center', valign: 'middle', sortable: true,},
        {title: '折扣', field: '折扣', align: 'center', valign: 'middle', sortable: true,},
        {
            title: '结账时间', field: 'rechargeTime', align: 'center', valign: 'middle', sortable: true, width: '12%',
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},
    ]
    return columns;
};

PaymentTotal.close = function () {
    // parent.layer.close(window.parent.Customer.layerIndex);
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}


/**
 * 检查是否选中
 */
PaymentTotal.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Recharge.seItem = selected[0];
        return true;
    }
};

/**
 * 查询预约客户列表
 */
PaymentTotal.search = function () {
    PaymentTotal.table.refresh({query: PaymentTotal.formParams()});
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/paymentTotal/sumBorrowAmount',
        data: PaymentTotal.formParams(),
        dataType: "json",
        success: function (data) {
            $('#sumMoney').html(data.borrowAmount_sum);
        }
    });
};

/**
 * 导出
 * @returns {{}}
 */

function downLoad() {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/paymentTotal/paymentExport?' + param;
}
var parseParam = function (param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function (i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};

PaymentTotal.resetSearch = function () {
    $("#customerName").val("");
    $("#customerPhone").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    $("#fieldTc").val("");
    $("#doctor").val("");
    $("#customerStatus").val("");
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    PaymentTotal.search();
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
PaymentTotal.formParams = function () {
    var queryData = {};
    queryData['customerName'] = $("#customerName").val();
    queryData['customerPhone'] = $("#customerPhone").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['doctor'] = $("#doctor").val();
    queryData['customerStatus'] = $("#customerStatus").val();
    return queryData;
}
var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}
$(function () {
    $.post(Feng.ctxPath + "/tc/getList", function (data) {
        var txt = '<option value="">--请选择TC--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
        }
        document.getElementById('fieldTc').innerHTML = txt;
        //状态单选
        var seletefieldTc = $('#fieldTcValue').val();
        if (seletefieldTc != "") {
            $("#fieldTc option[value=" + seletefieldTc + "]").attr("selected", "selected");
        }
    }, "json");
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    var defaultColunms = PaymentTotal.initColumn();
    var table = new BSTable(PaymentTotal.id, "/paymentTotal/getReport", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(PaymentTotal.formParams());
    PaymentTotal.table = table.init();
    PaymentTotal.search();
});

//***表格***
// var data_report = null;
// var myDate = new Date();
// var month;
// var day;
// if ((myDate.getMonth() + 1) < 10) {
//     month = "0" + (myDate.getMonth() + 1);
// } else {
//     month = "" + (myDate.getMonth() + 1);
// }
// if (myDate.getDate() < 10) {
//     day = "0" + myDate.getDate();
// } else {
//     day = "" + myDate.getDate();
// }
// document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
// document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
//
// function search_jztj() {
//     var customerName = $('#customerName').val();
//     var customerPhone = $('#customerPhone').val();
//     var startTime = $('#startTime').val();
//     var endTime = $('#endTime').val();
//     var fieldTc = $('#fieldTc').val();
//     var doctor = $('#doctor').val();
//     var customerStatus = $('#customerStatus').val();
//     $.ajax({
//         type: "post",
//         url: Feng.ctxPath + "/paymentTotal/getReport",
//         dataType: "json",
//         data: {
//             'customerName': customerName,
//             'customerPhone': customerPhone,
//             'startTime': startTime,
//             'endTime': endTime,
//             'fieldTc': fieldTc,
//             'doctor': doctor,
//             'customerStatus': customerStatus
//         },
//         success: function (data) {
//             console.log(data);
//             var arr = [];
//             data.forEach(function (v1) {
//                 var obj = {};
//                 obj['客户姓名'] = v1['客户姓名'];
//                 obj['电话'] = v1['电话'];
//                 obj['性别'] = v1['性别'];
//                 obj['年龄'] = v1['年龄'];
//                 obj['时间'] = v1['时间'];
//                 obj['收费人'] = v1['收费人'];
//                 obj['客户状态'] = v1['客户状态'];
//                 obj['TC'] = v1['TC'];
//                 // obj['医生'] = v1['医生'];
//                 Object.keys(v1).forEach(function (v2) {
//                     if (v2 !== '客户姓名' && v2 !== '电话' && v2 !== '性别' && v2 !== '年龄' && v2 !== '时间' && v2 !== '收费人' && v2 !== '客户状态' && v2 !== 'TC'   && v2 !== '备注' && v2 != 'customerId') {
//                         obj[v2] = v1[v2]
//                     }
//                 });
//                 obj['备注'] = v1['备注'];
//                 arr.push(obj);
//             });
//             data_report = arr;
//             renders();
//         },
//         error: function (msg) {
//             alert(msg);
//         }
//     });
// }
//
// //重置
// function resetSearch_jztj() {
//     document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
//     document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
//     var customerName = $('#customerName').val('');
//     var customerPhone = $('#customerPhone').val('');
//     var fieldTc = $('#fieldTc').val('--请选择');
//     var doctor = $('#doctor').val('');
//     var customerStatus = $('#customerStatus').val('全部');
// }
//
// function isRealNum(val) {
//     // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
//     if (val === "" || val == null) {
//         return false;
//     }
//     if (!isNaN(val)) {
//         return true;
//     } else {
//         return false;
//     }
// }
//
//
//
//
// //请求到数据渲染到页面
// function renders() {
//     var report_table = $("#report_table")[0];
//     var table_th = document.createElement("tr");
//     var str_th = '';
//     var ofragment = document.createDocumentFragment();
//     //刷新表
//     $("#report_table").empty();
//     //添加表头
//     table_th.class = 'table-bordered';
//     Object.keys(data_report[0]).forEach(function (v) {
//         str_th += "<th>" + v + "</th>";
//     });
//     table_th.innerHTML = str_th;
//     report_table.appendChild(table_th);
//     //添加表内容
//
//     // data_report.forEach(function (v1) {
//     //     var str_td = '';
//     //     var tr = document.createElement("tr");
//     //     Object.keys(v1).forEach(function (v2) {
//     //         str_td += "<td>" + v1[v2] + "</td>";
//     //     });
//     //     tr.innerHTML = str_td;
//     //     ofragment.appendChild(tr);
//     // });
//     for (var i = 0; i < data_report.length - 2; i++) {
//         var str_td = '';
//         var tr = document.createElement("tr");
//         Object.keys(data_report[i]).forEach(function (v2) {
//             str_td += "<td>" + data_report[i][v2] + "</td>";
//         });
//         tr.innerHTML = str_td;
//         ofragment.appendChild(tr);
//     }
//
//     var str_td1 = '';
//     var tr1 = document.createElement("tr");
//     Object.keys(data_report[data_report.length - 2]).forEach(function (v2) {
//         // str_td += "<td>" + data_report[q][v2] + "</td>";
//         var a = data_report[data_report.length - 2][v2];
//         var b = data_report[data_report.length - 1][v2];
//         if (isRealNum(a) && isRealNum(b)) {
//             str_td1 += "<td>" + (parseFloat(a) + parseFloat(b)) + "</td>";
//         } else {
//             str_td1 += "<td>" + a + "</td>";
//         }
//
//     });
//     tr1.innerHTML = str_td1;
//     ofragment.appendChild(tr1);
//
//
//     report_table.appendChild(ofragment);
// }
//
// $(function () {
//     search_jztj();
// })
